﻿using System.Windows.Media;
using Redbrick.Silverlight.Common;

namespace Moria.Common.Model
{
    public class Monster :  Drawable
    {

        #region Consts & Settings


        #endregion Consts & Settings

        #region Declarations

        int _hitPoints = 0;
        int _sleepCounter = 0;
        int _speed = 0;
        int _creatureIndex = -1;
        int _distanceToPlayer = -1;
        bool _isLit = false;
        int _stunnedCount = 0;
        bool _isConfused = false;

		  #endregion Declarations

        #region Constructor

        public Monster() { }

        public Monster(int id)
        {
            this.init(id);
        }
        public Monster(CreatureSpecies ctr)
        {
            int ndx = CreatureSpecies.Table.IndexOf(ctr);
            this.init(ndx);
        }

        public static implicit operator CreatureSpecies(Monster itm)
        {
            return itm.Creature;
        }
        public static implicit operator Monster(CreatureSpecies from)
        {
            return new Monster(from);
        }
        public static implicit operator Monster(int ItemNdx)
        {
            return new Monster(ItemNdx);
        }

        #endregion Constructor

        #region Methods

        private void init(int ndx)
        {
            this.CreatureIndex = ndx;

            if (this.Creature.HasMaxHP) this.HitPoints = this.Creature.HitDice.Max;
            else this.HitPoints = this.Creature.HitDice.Roll();
            this.Speed = this.Creature.Speed - 10;
            this.StunnedCount = 0;
            this.IsLit = false;
            if (this.Creature.Sleep == 0) this.SleepCounter = 0;
            else this.SleepCounter = (this.Creature.Sleep * 2) + Dice.RandInt((int)this.Creature.Sleep * 10);

        }
        public override string GetDescription()
        {
            return this.Creature.Name;
        }
        #endregion Methods

        #region Events


        #endregion Events

        #region Properties



        public int HitPoints
        {
            get { return this._hitPoints; }
            set
            {
                if (value == this._hitPoints) return;
                this._hitPoints = value;
                this.RaisePropertyChanged(() => this.HitPoints);
            }
        }				// Hit points		
        public int SleepCounter
        {
            get { return this._sleepCounter; }
            set
            {
                if (value == this._sleepCounter) return;
                this._sleepCounter = value;
                this.RaisePropertyChanged(() => this.SleepCounter);
            }
        }			// Inactive counter	
        public int Speed
        {
            get { return this._speed; }
            set
            {
                if (value == this._speed) return;
                this._speed = value;
                this.RaisePropertyChanged(() => this.Speed);
            }
        }					// Movement Speed	
        public int CreatureIndex
        {
            get { return this._creatureIndex; }
            set
            {
                if (value == this._creatureIndex) return;
                this._creatureIndex = value;
                this.RaisePropertyChanged(() => this.CreatureIndex);
            }
        }		// Pointer into creature

        public int DistanceToPlayer
        {
            get { return this._distanceToPlayer; }
            set
            {
                if (value == this._distanceToPlayer) return;
                this._distanceToPlayer = value;
                this.RaisePropertyChanged(() => this.DistanceToPlayer);
            }
        }	// Cur distance from player	
        public bool IsLit
        {
            get { return this._isLit; }
            set
            {
                if (value == this._isLit) return;
                this._isLit = value;
                this.RaisePropertyChanged(() => this.IsLit);
            }
        }
        public int StunnedCount
        {
            get { return this._stunnedCount; }
            set
            {
                if (value == this._stunnedCount) return;
                this._stunnedCount = value;
                this.RaisePropertyChanged(() => this.StunnedCount);
            }
        }
        public bool IsConfused
        {
            get { return this._isConfused; }
            set
            {
                if (value == this._isConfused) return;
                this._isConfused = value;
                this.RaisePropertyChanged(() => this.IsConfused);
            }
        }

        public CreatureSpecies Creature { get { return CreatureSpecies.Table[this.CreatureIndex]; } }


        #endregion Properties
    }
}
